<?php
// @formatter:off
/**
 * @file t.directory.lostthumbs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: t.directory.lostthumbs.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/tools/t.directory.lostthumbs.php $
 */

$wcToolScripts["directory.lostthumbs"]	= array (
	"name"		=> "Orphan thumbs",
	"description"	=> "Finds thumbs directoies and images of no valid comics",
	"class"		=> "WCToolDirectoryLostThumbs",
	"url"		=> "http://code.google.com/p/wcomix/",
	"enabled"	=> true
);
// @formatter:on

class WCToolDirectoryLostThumbs extends WCTool {
	protected $_previews = array();
	protected $_directories = array();

	public function run($spacer = "") {
		global $wcTagsHolder;
		global $wcSpecialTags;
		global $wcGroupsHolder;
		global $wcDefaults;
		global $wcDatabaseAccess;
		global $wcOptions;
		global $wcROOTDIR;
		global $wcPaths;
		global $wcGroupsHolder;

		$solve = false;
		if($wcDefaults["read-only"]) {
			echo $spacer.WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		} else {
			$solve = $wcOptions->activated("Solve");
		}

		$thumbsDir = "{$wcROOTDIR}/{$wcPaths["cache-directory-uri"]}/{$wcPaths["cache-thumbs-dirname"]}";

		$this->loadPreviews();

		$level = 4;
		if(isset($this->_previews[0])) {
			$level = count(explode("/", $this->_previews[0]));
		}
		$this->loadDirectoies($thumbsDir, $level);

		foreach($this->_previews as $key => $value) {
			$this->_previews[$key] = "$thumbsDir/{$value}";
		}

		foreach($this->_directories as $path) {
			if(is_dir($path)) {
				if(!in_array($path, $this->_previews)) {
					echo "{$spacer}Path '$path' belongs to no comic previews directory";
					if($solve) {
						$result = recursive_unlink($path);

						if(!count($result)) {
							echo ": ".WCColorText::ShellGreen("Solved")."\n";
						} else {
							echo ": ".WCColorText::ShellRed("Failed")."\n";
							foreach($result as $issue) {
								echo "{$spacer}\t- {$issue}\n";
							}
						}
					} else {
						echo ".\n";
					}
				}
			} elseif(is_file($path)) {
				$subpath = explode(".", $path);
				array_pop($subpath);
				$subpath = implode(".", $subpath);

				if(!in_array($subpath, $this->_previews)) {
					echo "{$spacer}Image path '$path' belongs to no comic previews";
					if($solve) {
						$result = recursive_unlink($path);

						if(!count($result)) {
							echo ": ".WCColorText::ShellGreen("Solved")."\n";
						} else {
							echo ": ".WCColorText::ShellRed("Failed")."\n";
							foreach($result as $issue) {
								echo "{$spacer}\t- {$issue}\n";
							}
						}
					} else {
						echo ".\n";
					}
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		global $wcDefaults;
		if(!$wcDefaults["read-only"]) {
			echo "{$spacer}Options:\n";
			echo "{$spacer}\t-s, --solve\n";
			echo "{$spacer}\t\tPhysically removes lost paths.\n\n";
		} else {
			echo "{$spacer}No options available.\n";
		}
	}
	protected function loadDirectoies($thumbsDir, $level) {
		$list = array($thumbsDir);
		$listaux = array();
		while($level) {
			foreach($list as $path) {
				if(!is_dir($path)) {
					continue;
				}

				$d = dir($path);
				while(false !== ($entry = $d->read())) {
					if($entry[0] != ".") {
						$auxpath = "{$path}/{$entry}";
						$listaux[] = $auxpath;
					}
				}
				$d->close();
			}

			$list = $listaux;
			$listaux = array();

			$level--;
		}

		$this->_directories = $list;
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;
			global $wcDefaults;

			if(!$wcDefaults["read-only"]) {
				if($wcOptions->addOption("Solve")) {
					$wcOptions->addOptionTrigger("Solve", "s");
					$wcOptions->addOptionTrigger("Solve", "solve");
				}

				$wcOptions->check();
			}

			$this->_optionsLoaded = true;
		}
	}
	protected function loadPreviews() {
		$stmtId = __CLASS__."::".__FUNCTION__;
		$stmtId = "{$stmtId}[group]";

		$query = "select *\n";
		$query .= "from   {$this->_dbprefix}comics\n";
		$query .= "where  cmc_lost      =  'N'\n";
		$query .= " and   cmc_generated =  'Y'\n";
		$query .= " and   cmc_thumbs    <> ''\n";
		$query .= "order by cmc_group asc\n";
		$stmt = $this->_db->prepare($stmtId, $query);

		if($stmt->execute()) {
			foreach($stmt->fetchAll() as $row) {
				$this->_previews[] = $row["cmc_group"].$row["cmc_thumbs"];
			}
		}
	}
}
?>